package com.songmao.pricecloudapitest.socket.websocket;

import com.alibaba.fastjson.JSON;
import com.songmao.pricecloudapitest.requestcore.ResponePoJo;
import com.songmao.pricecloudapitest.server.PriceCloudHttpCore;
import com.songmao.pricecloudapitest.socket.entity.websocket.UserEntity;
import lombok.SneakyThrows;
import org.java_websocket.WebSocket;
import org.springframework.stereotype.Component;
import org.testng.annotations.Test;

import java.net.URI;

/**
 * @author Remy
 * @description :
 * @date 2020-04-22 15:38
 */
@Component
public class Client extends PriceCloudHttpCore {


    public static WebSocketCore client;


    @SneakyThrows
    public void connection() {
        ResponePoJo rs = post("http://192.168.1.203:12958/auth", JSON.toJSONString(new UserEntity("client", "1MmH0AfGWnzd")));
        if (rs.getStatus() == 429) {
            throw new RuntimeException("Status=429,请求过于频繁;请稍后再试");
        }
        String url = "ws://192.168.1.203:12958/ws?token=" + rs.getData();
        client = new WebSocketCore(new URI(url));
        client.connect();
        while (!client.getReadyState().equals(WebSocket.READYSTATE.OPEN)) {
            System.out.println("正在连接...");
            client.reconnect();
        }
    }


    @Test
    public void test() {
        connection();
    }


}